package com.yihu.wlyy.web.doctor.scheduling;

import com.yihu.wlyy.entity.WlyyDoctorWorkTime;
import com.yihu.wlyy.service.app.scheduling.DoctorWorkTimeService;
import com.yihu.wlyy.web.BaseController;
import org.apache.commons.lang3.StringUtils;
import org.bouncycastle.jce.provider.asymmetric.ec.KeyFactory;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Calendar;
import java.util.List;

/**
 * Created by lyr on 2016/08/19.
 */
@Controller
@RequestMapping(value = "/doctor/work_scheduling")
public class DoctorWorkTimeController extends BaseController {

    @Autowired
    private DoctorWorkTimeService doctorWorkTimeService;

    /**
     * 查询医生工作时间设置
     *
     * @return
     */
    @RequestMapping(value = "/all")
    @ResponseBody
    public String getDoctorWorkTime() {
        try {
            JSONObject workTime = doctorWorkTimeService.findDoctorWorkTime(getUID());

            return write(200, "查询成功!", "data", workTime);
        } catch (Exception e) {
            e.printStackTrace();
            return error(-1, "查询失败!");
        }
    }

    /**
     * 获取医生某天工作时间
     *
     * @param week 某天
     * @return
     */
    @RequestMapping(value = "/week")
    @ResponseBody
    public String getDoctorWeekWorkTime(String week) {
        try {
            JSONObject workTime = doctorWorkTimeService.findDoctorWeekWorkTime(getUID(), week);

            return write(200, "查询成功!", "data", workTime);
        } catch (Exception e) {
            e.printStackTrace();
            return error(-1, "查询失败!");
        }
    }

    /**
     * 医生排班保存
     *
     * @param workScheduling 医生排班
     * @return
     */
    @RequestMapping(value = "/save")
    @ResponseBody
    public String saveDoctorWorkTime(String workScheduling) {
        try {
            if (StringUtils.isEmpty(workScheduling)) {
                return error(-1, "排班信息不能为空!");
            }

            JSONObject object = new JSONObject(workScheduling);

            if (!(object.has("workTime") && object.getJSONObject("workTime") != null)) {
                return error(-1, "工作时间段不能为空!");
            }

            JSONObject workTime = object.getJSONObject("workTime");

            if (StringUtils.isEmpty(workTime.getString("morningBegin"))) {
                return error(-1, "上午起始时间不能为空!");
            }
            if (StringUtils.isEmpty(workTime.getString("morningEnd"))) {
                return error(-1, "上午结束时间不能为空!");
            }
            if (StringUtils.isEmpty(workTime.getString("afternoonBegin"))) {
                return error(-1, "下午起始时间不能为空!");
            }
            if (StringUtils.isEmpty(workTime.getString("afternoonEnd"))) {
                return error(-1, "下午结束时间不能为空!");
            }
            if (StringUtils.isEmpty(workTime.getString("nightBegin"))) {
                return error(-1, "晚上起始时间不能为空!");
            }
            if (StringUtils.isEmpty(workTime.getString("nightEnd"))) {
                return error(-1, "晚上结束时间不能为空!");
            }

            if (doctorWorkTimeService.saveDoctorWorkTime(getUID(), object)) {
                return write(200, "保存成功!");
            } else {
                return write(-1, "保存失败!");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return error(-1, "保存失败!");
        }
    }


    /**
     * 设置医生是否接受咨询
     *
     * @param receiveConsult 是否接受咨询
     * @return
     */
    @RequestMapping(value = "/receive_consult_setting")
    @ResponseBody
    public String setDoctorReceiveConsult(String receiveConsult) {
        try {
            if(StringUtils.isEmpty(receiveConsult)){
                return error(-1,"是否接受咨询参数不能为空");
            }
            if (doctorWorkTimeService.setDoctorReceiveConsult(getUID(), receiveConsult)) {
                return error(200, "设置成功");
            } else {
                return error(-1, "设置失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return error(-1, "设置失败");
        }
    }

    /**
     * 查询某个医生是否在工作时间
     *
     * @param doctor 医生
     * @return
     */
    @RequestMapping(value = "/is_doctor_working")
    @ResponseBody
    public String isDoctorWorking(String doctor) {
        try {
            if(StringUtils.isEmpty(doctor)){
                return error(-1,"参数不能为空");
            }
            JSONObject result = doctorWorkTimeService.isDoctorWorking(doctor);
            return write(200, "查询成功", "data", result);
        } catch (Exception e) {
            e.printStackTrace();
            return error(-1, "查询失败");
        }
    }

    /**
     * 查询某个医生的工作时间
     *
     * @param doctor
     * @return
     */
    @RequestMapping(value = "/doctor_worktime")
    @ResponseBody
    public String getDoctorWorkTime(String doctor){
        try{
            JSONObject json = doctorWorkTimeService.findDoctorWorkTime(doctor);
            return write(200,"查询成功","data",json);
        }catch(Exception e){
            e.printStackTrace();
            return error(-1,"查询失败");
        }
    }

    /**
     * 查询某个医生某天的工作时间
     *
     * @param doctor 医生
     * @param week 周几
     * @return
     */
    @RequestMapping(value = "/doctor_week_worktime")
    @ResponseBody
    public String getDoctorWeekWorkTime(String doctor,String week){
        try{
            JSONObject json = doctorWorkTimeService.findDoctorWeekWorkTime(doctor,week);
            return write(200,"查询成功","data",json);
        }catch (Exception e){
            e.printStackTrace();
            return error(-1,"查询失败");
        }
    }
}
